package com.sskj.gocoin.v.activity;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.common.rxbus2.RxBus;
import com.sskj.gocoin.common.rxbus2.RxBusCode;
import com.sskj.gocoin.common.widget.ToolBarLayout;
import com.sskj.gocoin.p.activity.BindMobileActivityPresenter;
import com.sskj.gocoin.utils.CheckUtil;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.DisposUtil;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


public class BindMobileActivity extends BaseActivity<BindMobileActivityPresenter> {
    @BindView(R2.id.toolBar)
    ToolBarLayout toolBar;
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.etMobile)
    EditText etMobile;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.btSendCode)
    TextView btSendCode;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    private Disposable disposable;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_bind_mobile;
    }

    @Override
    public BindMobileActivityPresenter getPresenter() {
        return new BindMobileActivityPresenter();
    }

    public static void start(Context context) {
        Intent starter = new Intent(context, BindMobileActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void initView() {
        ClickUtil.click(btSendCode, () -> {
            String mobile = etMobile.getText().toString().trim();
            if (CheckUtil.isMobile(mobile)) {
                btSendCode.setEnabled(false);
                mPresenter.sendCode(mobile);
            } else {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.input_mobile_right));
            }
        });
        toolBar.setTitle(App.INSTANCE.getString(R.string.fabimineJavaBindMobileActivity1));
        toolBar.setLeftButtonText(App.INSTANCE.getString(R.string.fabimineJavaBindMobileActivity2));
        toolBar.setLeftButtonOnClickLinster(v -> finish());
        ClickUtil.click(btSubmit, () -> {
            String mobile = etMobile.getText().toString().trim();
            String code = etCode.getText().toString().trim();
            if (!CheckUtil.isMobile(mobile)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.input_mobile_right));
                return;
            }
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort(getString(R.string.bind_email_code_blank));
                return;
            }
            mPresenter.bindMobile(MyAppLication.getAccount(), mobile, code);
        });
    }


    public void sendCodeOk() {
        DisposUtil.close(disposable);
        disposable = Flowable.intervalRange(1, 60, 0, 1, TimeUnit.SECONDS)
                .map(i -> 60 - i)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(i -> {
                    btSendCode.setText(String.format(App.INSTANCE.getString(R.string.strRegisterActivity9), i));
                }, System.out::println, () -> {
                    btSendCode.setEnabled(true);
                    btSendCode.setText(getString(R.string.xmlactivity_register74));

                });

    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(disposable);
        super.onDestroy();
    }

    public void sendCodeError() {
        btSendCode.setEnabled(true);
    }

    public void bingSuccess() {
        setResult(RESULT_OK, new Intent().putExtra("resultData", etMobile.getText().toString()));
        RxBus.getDefault().send(RxBusCode.USER_INFO_UPDATE);
        finish();
    }
}
